package com.wcj.tools;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wcj
 * @Date 2021/6/29 15:55
 * @Version 1.0
 */
public class FileUtils {

    private static final String SOURCE_PATH = "./src/main/java/";

    private static final String CLASS_FILE_SUFFIX = ".java";

    private static List<String> classNames = new ArrayList<>();

    /**
     * 扫描指定包，获取包下的类的全类名
     *
     * @param packPath（com.wcj.entity这种）
     * @return
     */
    public static List<String> scanPackage(String packPath) {
        // 处理包名路径
        String path = SOURCE_PATH + packPath.replace(".", "/");
        // 递归获取包下类的全类名
        doScanPackage(path);
        return classNames;
    }

    private static void doScanPackage(String path) {
        File file = new File(path);
        if (!file.exists()) {
            throw new RuntimeException("file [" + path + "] is not exists!");
        }
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (File f : files) {
                doScanPackage(f.getPath());
            }
        }
        if (file.isFile() && file.getName().endsWith(CLASS_FILE_SUFFIX)) {
            String className = file.getPath().replace(SOURCE_PATH.replace("/", "\\"), "").replace("\\", ".").replace(CLASS_FILE_SUFFIX, "");
            System.out.println("Class  " + className);
            classNames.add(className);
        }
    }
}
